let event = {};
event.install = function (Vue) {
    if (event.installed === true) {
        return;
    }
    event.events = {};

    event.ignoreCase = true;

    event.names = {
        Open: "open",
        OpenChat: 'OpenChat',
        Receive: "Receive",
        Send: "Send",
        Online: "Online",
        Offline: "Offline",
        Read: "Read",
        Input: "Input",
        OnlineList: "OnlineList",
        Timer: "Timer",
    }

    event.on = function (key, fun) {
        if (event.ignoreCase === true) {
            key = key.toLowerCase();
        }
        if (typeof event.events[key] == "undefined") {
            event.events[key] = [];
        }
        let have = false;
        for (let i = 0; i < event.events[key].length; i++) {
            if (event.events[key][i] === fun) {
                have = true;
                break;
            }
        }
        if (!have) {
            event.events[key].push(fun);
        }
    }


    // 触发自定义事件
    event.trigger = function (key, data) {
        if (event.ignoreCase === true) {
            key = key.toLowerCase();
        }
        if (event.events[key]) {
            event.events[key].forEach(v => {
                v(data);
            })
        }
    }

    //移除自定义事件
    event.off = function (eventName, fn) {
        if (event.ignoreCase === true) {
            eventName = eventName.toLowerCase();
        }
        if (!event.events[eventName]) {
            return;
        }
        for (let i = 0; i < event.events[eventName].length; i++) {
            if (event.events[eventName][i] === fn) {
                event.events[eventName].splice(i, 1);
                break;
            }

        }
    }

    event.clear = function () {
        event.events = {};
    }

    Vue.prototype.$event = event;
    event.installed = true;
}
export default event;
